#
# SearchView.py
# (c) 2008 Will Baker.
#

from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtGui import QWidget, QTabWidget, QHBoxLayout, QVBoxLayout, QLineEdit, QPushButton, QSizePolicy

class SearchView(QWidget):
    """
    Widget for creating and submitting searches.
    """
    def __init__( self, parent=None ):
        QWidget.__init__( self, parent )
        
        search_tabs = QTabWidget( self )
        
        # Set up tag search widgets.
        tag_widget = QWidget( self )
        search_tabs.addTab( tag_widget, "Tags" )
        
        self.tag_search_box = QLineEdit( tag_widget )
        tag_search_button = QPushButton( "Search", tag_widget )
        
        previous_button = QPushButton( "Previous", self )
        next_button = QPushButton( "Next", self )
        
        # Set up widget layout
        top_layout = QVBoxLayout()
        
        hbox_layout = QHBoxLayout()
        hbox_layout.addWidget( self.tag_search_box )
        hbox_layout.addWidget( tag_search_button )
        tag_widget.setLayout( hbox_layout )
        
        top_layout.addWidget( search_tabs )
        
        hbox_layout = QHBoxLayout()
        hbox_layout.addWidget( previous_button )
        hbox_layout.addWidget( next_button )
        top_layout.addLayout( hbox_layout )
        
        self.setLayout( top_layout )
        
        # Set up connections.
        QObject.connect( self.tag_search_box, SIGNAL("returnPressed()"), self.on_search_submitted )
        QObject.connect( tag_search_button,   SIGNAL("clicked()"),       self.on_search_submitted )
        QObject.connect( previous_button,     SIGNAL("clicked()"),       self.on_previous_page )
        QObject.connect( next_button,         SIGNAL("clicked()"),       self.on_next_page )
        
    def on_search_submitted( self ):
        """
        Submit a search string.
        """
        text = self.tag_search_box.text()
        if text != "":
            self.tag_search_box.setText( "" )
            self.emit( SIGNAL("search_submitted"), str(text) )
            
    def on_previous_page( self ):
        self.emit( SIGNAL("previous_page") )
            
    def on_next_page( self ):
        self.emit( SIGNAL("next_page") )
